/**
 *
 * Copyright 2012 TeleNav, Inc. All rights reserved.
 * UrlsOfServer.java
 *
 */
package com.telenav.snowsweeper;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 *@author hhzhu
 *@date 2012-12-7
 */
public class UrlsOfServer
{
    public static final int TYPE_CONFIG_MAP = 1;
    public static final int TYPE_CONFIG_PASS_POINT = 2;
    public static final int TYPE_CONFIG_HINT = 3;
    public static final int TYPE_CONFIG_EVALUATION = 4;
    
    private String SITE = "http://dysong-pc.china.telenav.com:8080/snowsweeper";
    private String MAP = "/map";
    private String PASS_POINT = "/passpoint";
    private String HINT = "/hint";
    private String EVALUATION = "/evaluation";
    private String GOOGLE = SITE + "/loc/json";
    private Context context;
    private static UrlsOfServer instance = new UrlsOfServer();
    
    private UrlsOfServer()
    {
    }
    
    public static UrlsOfServer getInsttance()
    {
        return instance;
    }
    
    public synchronized void loadServerConfig(Context context)
    {
        this.context = context;
        SharedPreferences sharedPreferences = context.getSharedPreferences("MY_SSERVER_CONFIG", Context.MODE_PRIVATE);
        SITE = sharedPreferences.getString("SITE", SITE);
    }
    
    public synchronized boolean setSiteConfig(String url)
    {
        if (url != null && url.startsWith("http://"))
        {
            SharedPreferences sharedPreferences = context.getSharedPreferences("MY_SSERVER_CONFIG", Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            editor.putString("SITE", url);
            editor.commit();
            SITE = url;
            return true;
        }
        return false;
    }
    
    public synchronized String getURLByType(int type)
    {
        SharedPreferences sharedPreferences = context.getSharedPreferences("MY_SSERVER_CONFIG", Context.MODE_PRIVATE);
        SITE = sharedPreferences.getString("SITE", SITE);
        switch (type)
        {
            case TYPE_CONFIG_MAP:
                return SITE + MAP;
            case TYPE_CONFIG_PASS_POINT:
                return SITE + PASS_POINT;
            case TYPE_CONFIG_HINT:
                return SITE + HINT;
            case TYPE_CONFIG_EVALUATION:
                return SITE + EVALUATION;
        }
        return "";
    }
    
    public static String connServerForResult(String strUrl) {   
        HttpGet httpRequest = new HttpGet(strUrl);   
        String strResult = "";   
        try {   
            HttpClient httpClient = new DefaultHttpClient();   
            HttpResponse httpResponse = httpClient.execute(httpRequest);   
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {   
                strResult = EntityUtils.toString(httpResponse.getEntity());   
            }   
        } catch (ClientProtocolException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return strResult;   
    }  
    
    public String getSite()
    {
        return SITE;
    }
}
